/**
 * 存储localStorage
 */
export const setStore = (key = "", data = "", token = "") => {
  if (!key) {
    return false;
  }
  if (!token) {
    return false;
  }
  try {
    uni.setStorageSync(key, data);
  } catch (e) {
    console.error(`Error setting ${key} in storage:`, e);
  }
};

/**
 * 获取localStorage
 */
export const getStore = (key = "") => {
  if (!key) {
    return "";
  }
  try {
    return uni.getStorageSync(key);
  } catch (e) {
    console.error(`Error getting ${key} from storage:`, e);
    return "";
  }
};

/**
 * 获取token
 */
// export const getToken = () => {
//   return getStore("Authorization");
// };

/**
 * 清理localStorage
 */
export const clearStore = () => {
  try {
    uni.clearStorageSync();
  } catch (e) {
    console.error("Error clearing storage:", e);
  }
};

/**
 * 删除指定key的localStorage
 */
export const delStore = (key) => {
  if (!key) {
    return false;
  }
  try {
    uni.removeStorageSync(key);
    return true;
  } catch (e) {
    console.error(`Error removing ${key} from storage:`, e);
    return false;
  }
};

/**
 * 存储用户信息
 */
export const setUserInfo = (userInfo, token) => {
  if (!userInfo || !token) {
    return false;
  }
  Object.keys(userInfo).forEach((key) => {
    setStore(key, userInfo[key], token);
  });
  return true;
};

/**
 * 获取用户信息
 */
export const getUserInfo = () => {
  const keys = [
    "userName",
    "name",
    "entName",
    "orgName",
    "entOperator",
    "ygUserInfo",
    "whUserInfo",
    "userType",
    "entType",
    "staffType",
    "whLoginState",
    "ygLoginState",
    "perCodeList",
    "tenantId",
    "userId",
    "zwUserInfo",
    "zwLoginState",
    "areaCode",
    "Authorization",
    "TokenType",
  ];
  const userInfo = {};
  keys.forEach((key) => {
    userInfo[key] = getStore(key);
  });
  return userInfo;
};

export function removeStore(key) {
  uni.removeStorageSync(key);
}
export const getTokenType = () => {
  return getStore("TokenType");
};
//获取token
export const getToken = () => {
  return getStore("Authorization");
};

// 添加这个新方法
export const clearUserData = () => {
  const keysToRemove = [
    "Authorization",
    "refreshToken",
    "getTokenType",
    "userName",
    "name",
    "entName",
    "orgName",
    "entOperator",
    "ygUserInfo",
    "whUserInfo",
    "userType",
    "entType",
    "staffType",
    "whLoginState",
    "ygLoginState",
    "perCodeList",
    "tenantId",
    "userId",
    "zwUserInfo",
    "zwLoginState",
    "areaCode",
    "rememberedUsername",
    "rememberedPassword",
  ];

  keysToRemove.forEach((key) => removeStore(key));
};

export default {
  state: {
    token: null,
    tokenType: null,
    // ... 其他状态
  },
  mutations: {
    setToken(state, token) {
      state.token = token;
      setStore("Authorization", token);
    },
    setTokenType(state, tokenType) {
      state.tokenType = tokenType;
      setStore("TokenType", tokenType);
    },
    // ... 其他 mutations
  },
  getters: {
    getToken: (state) => state.token || getStore("Authorization"),
    getTokenType: (state) => state.tokenType || getStore("TokenType"),
    // ... 其他 getters
  },
  // ... 其他部分
};
